<?php

require MODULE_ROOT . '/helpers/functions.php';
global $_GPC, $_W;
$operation = $op = ($_GPC['op'] ?: 'index');

// 公共变量
$user = user();
$config = config();
$uniacid = uniacid();
$group = $user['group'] ? get_group_info_by_id($user['group']) : false;

check_auth();

if($op == 'index') {
    $classId = input('class') + 0;
    $where = [
        "uniacid = {$uniacid}",
    ];
    $sql = 'SELECT * FROM ' . tablename('d1sj_card_newclass') . ' WHERE ' . implode(' AND ', $where);
    $classes = pdo_fetchall($sql);
    $banners = get_banner_by_type('news');
    include $this->template('news');
} else if($op == 'ajax') {
    $classId = input('class') + 0;
    $where = [
        "uniacid = {$uniacid}",
    ];
    if($classId)
        array_push($where, "newclass_id = $classId");

    $count = 'SELECT count(id) FROM ' . tablename('d1sj_card_news') . ' WHERE ' . implode(' AND ', $where);
    $total = get_page_total($count);

    $sql = 'SELECT * FROM ' . tablename('d1sj_card_news') . ' WHERE ' . implode(' AND ', $where) . ' ORDER BY sort desc ' . get_limit();
    $list = pdo_fetchall($sql);
    include $this->template('news');
} else if($op == 'detail') {
    $sql = 'SELECT * FROM ' . tablename('d1sj_card_news') . ' WHERE ID = :id';

    $detail = pdo_fetch($sql, [':id' => input('id')]);


    $detail['content'] = htmlspecialchars_decode($detail['content']);
    $userWeight = $group['weight'] ?: 0;
    if($userWeight < $detail['weight'])
         message('权限不足，请购买合伙人', $this->createMobileUrl('vip'), 'error');

    $detail['click']++;
    add_click(input('id'), $detail['click']);

    include $this->template('news');
}


function add_click($id, $click = null)
{
    $click = $click ?: 0;
    pdo_update('d1sj_card_news', ['click' => $click], ['id' => $id]);
}
